package com.xq.demo;

import cn.hutool.http.HttpUtil;
import com.google.common.collect.Maps;

import java.io.*;
import java.nio.charset.Charset;

public class JavaToPdfHtml2 {




    public static void main(String[] args) throws Exception {


        long start = System.currentTimeMillis();
        String result2 = parseHtml2Pdf("https://pay.weixin.qq.com/wiki/doc/api/index.html");
        long all = System.currentTimeMillis()- start;
        System.out.println("pdf生成地址:"+result2+",用时:"+all/1000+"秒");
    }

    public static String parseHtml2Pdf(String url) throws IOException {
        System.out.println(url);
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec("D://install/phantomjs2/bin/phantomjs D://install/phantomjs2/bin/html2pdf.js "+url);
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuffer sbf = new StringBuffer();
        String tmp = "";
        while ((tmp = br.readLine()) != null) {
            sbf.append(tmp);
        }
        String resultstr = sbf.toString();
        System.out.println("resultstr:"+resultstr);
        String[] arr = resultstr.split("\\$");
        String result = "";
        for(String s : arr){
            if(s.endsWith("pdf"))result = s;
        }
        return result;
    }

}
